package com.yiding.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiding.sys.common.enums.ResultCodeEnum;
import com.yiding.sys.common.model.PageResult;
import com.yiding.sys.dao.OrderInfoMapper;
import com.yiding.sys.pojo.OrderInfo;
import com.yiding.sys.pojo.bo.OrderInfoQueryParamsBO;
import com.yiding.sys.pojo.vo.OrderDetailsVO;
import com.yiding.sys.pojo.vo.OrderListVO;
import com.yiding.sys.service.OrderInfoService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author War horse imwarhorse@aliyun.com
 * Date: 2020/8/7
 * Description: No Description
 */

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

	@Resource
	private OrderInfoMapper orderInfoMapper;

	@Override
	public PageResult<OrderListVO> getOrderList(Map<String, Object> params) {
		Page<OrderInfo> page = new Page<>(MapUtils.getInteger(params, "page", 1), MapUtils.getInteger(params, "limit", 10));

		// id, orderId, customerPhone(title), name(brand), status
		List<OrderInfo> orderInfoList = orderInfoMapper.selectAll(page, params);

		List<OrderListVO> data = this.entity2Vo(orderInfoList);

		return PageResult.<OrderListVO>builder().code(ResultCodeEnum.SUCCESS.getCode()).count(page.getTotal()).data(data).build();
	}

	@Override
	public OrderDetailsVO getOrderInfoById(Long id) {
		OrderInfo orderInfo = this.getById(id);
		OrderDetailsVO detailsVO = new OrderDetailsVO();
		BeanUtils.copyProperties(orderInfo, detailsVO);
		return detailsVO;
	}

	@Override
	public PageResult<OrderListVO> queryOrderList(OrderInfoQueryParamsBO queryParamsBO, Integer pageNo, Integer pageSize) {
		// 分页相关参数对象
		Page<OrderInfo> page = new Page<>(pageNo, pageSize);
		Map<String, Object> params = new HashMap<>();
		params.put("page", pageNo);
		params.put("limit", pageSize);

		OrderInfo queryObj = new OrderInfo();
		queryObj.setId(queryParamsBO.getQueryId());
		queryObj.setOrderId(queryParamsBO.getQueryOrderId());
		queryObj.setCustomerPhone(queryParamsBO.getQueryTitle());
		queryObj.setName(queryParamsBO.getQueryBrand());
		queryObj.setStatus(queryParamsBO.getQueryStatus());

		List<OrderInfo> orderInfoList = orderInfoMapper.selectByQueryParams(page, params, queryObj);

		// Bean Convert
		List<OrderListVO> data = this.entity2Vo(orderInfoList);

		return PageResult.<OrderListVO>builder().code(ResultCodeEnum.SUCCESS.getCode()).count(page.getTotal()).data(data).build();
	}


	/**
	 * List\<OrderInfo\>转为List\<OrderListVO\>
	 * TODO 寻找不需要循环拷贝的办法，以防止在source容器元素数量较大时出现性能消耗过大的问题
	 *
	 * @param orderInfoList
	 * @return
	 */
	private List<OrderListVO> entity2Vo(List<OrderInfo> orderInfoList) {
		if (orderInfoList == null || orderInfoList.size() < 1) return Collections.emptyList();

		List<OrderListVO> result = new ArrayList<>();
		for (OrderInfo orderInfo : orderInfoList) {
			OrderListVO orderListVO = new OrderListVO();
			BeanUtils.copyProperties(orderInfo, orderListVO);
			result.add(orderListVO);
		}

		return result;
	}
}
